<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 提示信息的组件 -->
      <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>
      <!-- 步骤条的组件 -->
      <el-steps :active="activeName-0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- 表单的组件 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
        label-position="top"
      >
        <!-- tab标签 组件 -->
        <el-tabs v-model="activeName" @tab-click="handleClick" tab-position="left">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="addForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addForm.goods_weight" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addForm.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <!-- 级联选择器 -->
              <el-cascader
                v-model="addForm.goods_cat"
                :options="cateList"
                :props="cascaderProp"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-form-item :label="item.attr_name" v-for="item in manyTableDate" :key="item.attr_id">
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox border :label="cb" v-for=" (cb,i) in item.attr_vals" :key="i"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item :label="item.attr_name" v-for="item in onlyTableDate" :key="item.attr_id">
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              :action="uploadUrl"
              class="upload-demo"
             
              :on-remove="handleRemove"
              list-type="picture"
              :headers="headersObj"
              :on-success="handerSuccess"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="addForm.goods_introduce" ref="myQuillEditor"></quill-editor>
            <el-button type="primary" style="margin-top:15px">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>

    <!-- 弹框 -->
    <el-dialog title="图片预览" :visible.sync="dialogVisible" width="40%" >
        <img :src="previewImg" alt="" class="previmg">
    </el-dialog>
  </div>
</template>

<script>
import config from "../../config.json";
export default {
  data() {
    return {
      activeName: "0",
      // 添加的表单
      addForm: {
        // 商品名字
        goods_name: "",
        // 商品所属分类
        goods_cat: [],
        // 商品价格
        goods_price: "",
        // 商品数量
        goods_number: "",
        // 商品重量
        goods_weight: "",
        // 商品介绍
        goods_introduce: "",
        // 商品图片
        pics: [],
        // 商品参数信息
        attrs: []
      },
      //添加表单的规则
      addFormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" }
        ],
        goods_cat: [
          { required: true, message: "请输入商品分类", trigger: "blur" }
        ]
      },
      // 商品分类数组
      cateList: [],
      // 指定之间的关系
      cascaderProp: {
        label: "cat_name",
        value: "cat_id",
        children: "children"
      },
      // 动态参数数组
      manyTableDate: [],
      // 静态属性的数组
      onlyTableDate: [],
      uploadUrl: config.baseURL + "upload",
      headersObj: {
        Authorization: window.sessionStorage.getItem("token")
      },
      previewImg:"",
      dialogVisible:false
    };
  },
  methods: {
    // 获取商品所有分类
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 3 }
      });
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败");
      }
      // 把获取的数据放到新的数组中
      this.cateList = res.data;
    },
    // 级联选择器发生改变
    handleChange() {
      if (this.addForm.goods_cat.length === 3) {
        console.log(this.addForm.goods_cat);
      } else {
        this.addForm.goods_cat = [];
      }
    },
    async handleClick() {
      if (this.activeName == 1) {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          { params: { sel: "many" } }
        );
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("获取动态参数失败");
        }
        res.data.forEach(item => {
          item.attr_vals = item.attr_vals ? item.attr_vals.split(" ,") : [];
        });
        this.manyTableDate = res.data;
      } else if (this.activeName == 2) {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          { params: { sel: "only" } }
        );
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("获取动态参数失败");
        }
        this.onlyTableDate = res.data;
        console.log(this.onlyTableDate);
      }
    },
    // 上传
    handlePreview(result) {
      this.previewImg=result.response.data.url;
      this.dialogVisible=true
    },
    handleRemove(result) {
      console.log(result);
      const picPath = result.response.data.tmp_path;
      const i = this.addForm.pics.findIndex(x => x.pic === picPath);
      this.addForm.pics.slice(i, 1);
      console.log(this.addForm);
    },
    handerSuccess(result) {
      console.log(result);
      if (result.meta.status == 200) {
        const photo = { pic: result.data.tmp_path };
        this.addForm.pics.push(photo);
        console.log(this.addForm);
      }
    }
  },
  created() {
    this.getCateList();
  },
  computed: {
    cateId: function() {
      if (this.addForm.goods_cat.length === 3) {
        return this.addForm.goods_cat[2];
      }
      return null;
    }
  }
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.el-steps {
  margin: 15px;
}
.previmg{
  width: 100%;
}
</style>

